/*
 * Copyright (c) 2017, www.zm0618.com All Rights Reserved..
 */

package com.light.hessian.provider.discovery;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhaojun on 2017/02/10.
 */
public class ProviderRegistryConsulConfParser implements ProviderRegistryConfParser{
    @Override
    public void doParse(AbstractProviderDiscoverer discoverer) {

        /**
             mics.provider.discovery.type=consul             可选  默认当local方式
             mics.provider.client.balance=roundrobin         可选     默认是roundrobin
             mics.provider.registry.nodes=192.168.10.21:8500 #注册中心地址
             mics.provider.registry.transport.protocol=http            #注册中心访问协议
             mics.provider.registry.token=abcd1233           #注册中心token
             mics.provider.discovery.interval=10s            #
             mics.client.balancer.show=true            #    是否打印balancer信息
         */
        Properties registryConfProperties = discoverer.getRegistryConfProperties();
        String provider=discoverer.getProvider();
        String nodes=registryConfProperties.getProperty(String.format("%s.provider.registry.nodes",provider),"localhost:8500");
        String []node= StringUtils.split(nodes,",");
        List<String> nodeList=new ArrayList<String>();
        Collections.addAll(nodeList,node);
        String protocol=registryConfProperties.getProperty(String.format("%s.provider.registry.protocol",provider),"http");
        String transportProtocol=registryConfProperties.getProperty(String.format("%s.provider.registry.transport.protocol",provider),"http");
        String token=registryConfProperties.getProperty(String.format("%s.provider.registry.token",provider));
        String discoveryInterval=registryConfProperties.getProperty(String.format("%s.provider.discovery.interval",provider),"10s");
        String showBalancer=registryConfProperties.getProperty(String.format("%s.client.balancer.show",provider),"false");


//      if (StringUtils.equals(protocol,"hessian")) {//编码协议
//      }
        HessianConsulDiscoverReference consulDiscoverReference=new HessianConsulDiscoverReference();
        consulDiscoverReference.setProvider(provider);
        consulDiscoverReference.setProtocol(protocol);
        consulDiscoverReference.setTransProctocol(transportProtocol);
        consulDiscoverReference.setToken(token);
        consulDiscoverReference.setInterval(getSecondNum(discoveryInterval));
        consulDiscoverReference.setNodes(nodeList);
        consulDiscoverReference.setShowBalancer(Boolean.valueOf(showBalancer));
        discoverer.handleDiscoveryReference(consulDiscoverReference);
    }


    private static int getSecondNum(String  secondConf){
        Pattern secondPatten=Pattern.compile("^(\\d)+s$");
        Matcher matcher = secondPatten.matcher(secondConf);
        if (matcher.find()){
            String g = matcher.group();
            String secondNum = g.substring(0, g.length() - 1);
            return Integer.parseInt(secondNum);
        }
        return 0;
    }
}
